package com.boesch.electroOffice.dataaccess.daoImpl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.boesch.electroOffice.dataaccess.common.IllegalDataAccessException;
import com.boesch.electroOffice.dataaccess.common.ReturnCodesDataaccess;
import com.boesch.electroOffice.dataaccess.dao.IdentifierDao;
import com.boesch.electroOffice.dataaccess.mapper.IdentifierMapper;
import com.boesch.electroOffice.model.electroOffice.Identifier;

/**
 * This class executes the sql statements on the electroOffice database for class Identifier.
 * If a Exception is thrown, it will be unpacked to a illegalDataAccessException 
 * including a return code which can be shown to the user.
 * 
 * @author Ralph Koster
 * 
 */
@Repository
public class IdentifierDaoImpl implements IdentifierDao, Serializable {

	private static final long serialVersionUID = 8721991891812497626L;

	@Autowired
	private JdbcTemplate eoJdbcTemplate;

	@Value("${electroOffice.queryReadIdentifierById}")
	private String queryReadIdentifierById;
	@Value("${electroOffice.queryReadIdentifierByEntityAndIdentifier}")
	private String queryReadIdentifierByEntityAndIdentifier;
	@Value("${electroOffice.queryReadAllIdentifiers}")
	private String queryReadAllIdentifiers;
	@Value("${electroOffice.queryReadIdentifiersByEntity}")
	private String queryReadIdentifiersByEntity;
	@Value("${electroOffice.queryReadIdentifiersByEntityAndSubentity}")
	private String queryReadIdentifiersByEntityAndSubentity;

	/**
	 * {@inheritDoc}}
	 */
	public Identifier getIdentifierById(int id) throws IllegalDataAccessException {
		Identifier identifier = null;
		List<Identifier> identifiers = eoJdbcTemplate.query(queryReadIdentifierById, new Object[] { id }, new IdentifierMapper());
		if (null != identifiers && identifiers.size() == 1) {
			identifier = identifiers.get(0);
			return identifier;
		} else {
			throw new IllegalDataAccessException(ReturnCodesDataaccess.OBJECT_NOT_FOUND_ERROR.getValue());
		}
	}

	/**
	 * {@inheritDoc}}
	 */
	public Identifier getIdentifierByEntityAndIdentifier(String entity, String identifier) throws IllegalDataAccessException {
		Identifier identif = null;
		List<Identifier> identifiers = eoJdbcTemplate.query(queryReadIdentifierByEntityAndIdentifier, new Object[] { entity, identifier }, new IdentifierMapper());
		if (null != identifiers && identifiers.size() == 1) {
			identif = identifiers.get(0);
			return identif;
		} else {
			throw new IllegalDataAccessException(ReturnCodesDataaccess.OBJECT_NOT_FOUND_ERROR.getValue());
		}
	}

	/**
	 * {@inheritDoc}}
	 */
	public List<Identifier> readAllIdentifiers() throws IllegalDataAccessException {
		List<Identifier> identifiers = new ArrayList<Identifier>();
		try {
			identifiers = eoJdbcTemplate.query(queryReadAllIdentifiers, new Object[] {}, new IdentifierMapper());
			return identifiers;
		} catch (DataAccessException e) {
			throw new IllegalDataAccessException(ReturnCodesDataaccess.NO_OBJECTS_FOUND_ERROR.getValue(), e);
		}
	}

	/**
	 * {@inheritDoc}}
	 */
	public List<Identifier> readIdentifiersByEntity(String entity) throws IllegalDataAccessException {
		List<Identifier> identifiers = new ArrayList<Identifier>();
		try {
			identifiers = eoJdbcTemplate.query(queryReadIdentifiersByEntity, new Object[] { entity }, new IdentifierMapper());
			return identifiers;
		} catch (DataAccessException e) {
			throw new IllegalDataAccessException(ReturnCodesDataaccess.NO_OBJECTS_FOUND_ERROR.getValue(), e);
		}
	}

	/**
	 * {@inheritDoc}}
	 */
	public List<Identifier> readIdentifiersByEntityAndSubentity(String entity, String subentity) throws IllegalDataAccessException {
		List<Identifier> identifiers = new ArrayList<Identifier>();
		try {
			identifiers = eoJdbcTemplate.query(queryReadIdentifiersByEntityAndSubentity, new Object[] { entity, subentity }, new IdentifierMapper());
			return identifiers;
		} catch (DataAccessException e) {
			throw new IllegalDataAccessException(ReturnCodesDataaccess.NO_OBJECTS_FOUND_ERROR.getValue(), e);
		}
	}
}
